/**
 * Copyright (c) 2009, http://www.softwareinlife.com. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, 
 * with or without modification, are permitted provided that 
 * the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice, 
 *    this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution.
 * 
 * 3. Neither the name of the http://www.softwareinlife.com nor the names of its 
 *    contributors may be used to endorse or promote products derived from 
 *    this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE SOFTWAREINLIFE AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. 
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.softwareinlife.vision.android.communication.sync;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.EntityTemplate;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import com.softwareinlife.vision.android.R;
import com.softwareinlife.vision.android.communication.SyncXmlParser;
import com.softwareinlife.vision.android.communication.model.VisionSoft;

/**
 * @author Cho, jae-young (greenfrog@softwareinlife.com)
 * @date 2009. 10. 19.
 */
public class SyncManager {
	
	protected final String SYNC_ADDR = 
			"http://app.allsolution.kr/api/sync.vs";
	
	protected final String SYNC_DATE = "yyyy-MM-dd hh:mm:ss";
	
	protected final String USER_ID = "userId";
	protected final String LAST_SYNC_DATE = "lastSyncDate";
	protected final String SYNC_DURATION = "syncDuration";
	
	protected HttpClient m_httpClient = new DefaultHttpClient();
	protected HttpPost m_httpPostRequest = new HttpPost(SYNC_ADDR);
	protected HttpResponse m_httpResponse = null;
	

	public HttpResponse doSync(VisionSoft arg_visionSoft, String arg_userId, 
			String arg_syncDuration)
	{
		initHeader(arg_userId, arg_syncDuration);
		initBody(arg_visionSoft);
		
		try {
			m_httpResponse = m_httpClient.execute(m_httpPostRequest);
			
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			Log.e("SyncManager.doSync", e.getMessage());
			return null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Log.e("SyncManager.doSync", e.getMessage());
			return null;
		}
		return m_httpResponse; 
	}

	protected void initHeader(String arg_userId, String arg_syncDuration)
	{
		m_httpPostRequest.addHeader(USER_ID, arg_userId);
		
		Calendar calendar = new GregorianCalendar();
		SimpleDateFormat dateFormat = new SimpleDateFormat(SYNC_DATE);
		m_httpPostRequest.addHeader(LAST_SYNC_DATE, dateFormat.format(
				calendar.getTime()));
		m_httpPostRequest.addHeader(SYNC_DURATION, arg_syncDuration);
	}
	
	protected void initBody(VisionSoft arg_visionSoft)
	{
		String resultOfGenerateXml = SyncXmlParser.generate(arg_visionSoft);
		
		ContentProducerImpl contentProducerImpl = 
				new ContentProducerImpl(resultOfGenerateXml);
		
		HttpEntity httpEntity = new EntityTemplate(contentProducerImpl);
		m_httpPostRequest.setEntity(httpEntity);
	}
}
